package cate.game.mail;

import com.google.common.base.Stopwatch;
import cp.solution.business.gm.GmMailEntity;
import easy.java.struct.ref.IntRef;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
public class GmMailHandler {

    private MailManager manager;
    private MailDataSource mds;

    public GmMailHandler(MailManager manager, MailDataSource mds) {
        this.manager = manager;
        this.mds = mds;
    }

    public List<GmMailEntity> getGmMailList() {
        return mds.getGmMailList();
    }

    public void onUpdate(List<GmMailEntity> mailList) {
        try {
            IntRef n = new IntRef();
            Stopwatch stopwatch = Stopwatch.createStarted();
            this.manager.getGame().role.activeForOnline(role -> {
                role.getMail().gm.scan(mailList);
                n.v++;
            });
            log.info("处理GM邮件更新，邮件总数={} 耗时={}ms", mailList.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
